﻿#region Header
/********************************************************           
 * VisualizationDataRequestFactory         
 *                                                         
 * Author:  Richard Barclay  (phphanatic at gmail dot com) 
 * 
 * Copyright: 2009 Richard Barclay
 * 
 * License: GNU General Public License (GPL)
 *     
 * This file is part of the Intergr8.GoogleVisualization library.
 *
 *   The Intergr8.GoogleVisualization library is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *  Intergr8.GoogleVisualization library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Intergr8.GoogleVisualization library.  If not, see <http://www.gnu.org/licenses/>.
 *                                                         
 * Purpose:  Create valid populated ResponseMessage object and return the IResponseMessage interface
 *                                                         
 * Usage:                                                  
 *     
 ********************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Intergr8.GoogleVisualization;
using Intergr8.GoogleVisualization.Interfaces;

namespace Intergr8.GoogleVisualization.DataFactory
{
    /// <summary>
    /// The VisualizationDataRequestFactory provides an interface between a service or application serving data and the data retreival mechanism.
    /// </summary>
    /// <remarks>
    /// Generation of a RequestHandler object, containing the detail of a parse request is performed by the CaptureRequest method. Within this method, the query parameters
    /// are parsed and an IRequestHandler reference returned.
    /// 
    /// Generation of a ResponseMessage object, containing the retrieved data and response attributes is generated by the CreateResponse method. This method uses a datasource name string to 
    /// identify where to fetch data from and the state of the RequestHandler reference to describe the operations performed on the data. A completed ResponseMessage reference is returned.
    /// </remarks>
    public class VisualizationDataRequestFactory
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="datasource"></param>
        /// <returns></returns>
        public static IResponseMessage CreateResponse(IRequestHandler request, string datasource)
        {           
           // string jsonDataString = "{cols: [{id: 'STYLE', label: 'Programming Style', type: 'string'}, {id: 'FUN', label: 'Fun', type: 'number'}, {id: 'WORK', label: 'Work', type: 'number'}], rows: [{c: [{v: 'Hand Coding'}, {v: 30}, {v: 200}]}, {c: [{v: 'Using the .NET Library'}, {v: 300}, {v: 10}]}, {c: [{v: 'Skipping Visualization'}, {v: -50}, {v: 0}]}]}";

            IResponseMessage respMessage = null;
            try
            {
                // Create a Data Source manager to retreive data from the selected Provider
                Intergr8.Data.DataManagement.DataSourceManager dsm = new Intergr8.Data.DataManagement.DataSourceManager();
                // Fetch the data from the selected Provider
                System.Data.DataTable data = dsm.Fetch(datasource);

                // Transform  the data in the required response format
                IResponseData respData = ResponseData.GenerateResponseData(data);

                // Wrap it all up in a formatted message
                 respMessage = ResponseMessage.CreateResponse(request, respData);
            }
            catch (Exception ex)
            {
                // Generate a Response indicating that an error occurred
                respMessage = ResponseMessage.CreateResponse(request, ex);
            }
            return respMessage;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tq"></param>
        /// <param name="tqx"></param>
        /// <returns></returns>
        public static IRequestHandler CaptureRequest(string tq, string tqx)
        {
            IRequestHandler reqH = RequestHandler.CreateRequestHandler(tq, tqx);

            return reqH;
        }

        public static IRequestHandler CaptureRequest(HttpRequest httpRequest)
        {
            IRequestHandler reqH = RequestHandler.CreateRequestHandler(httpRequest.QueryString);

            return reqH;
        }
    }
}
